package cn.am.controller;

import cn.am.pojo.InterestPoint;
import cn.am.pojo.PlanDTO;
import cn.am.pojo.PlanVO;
import cn.am.service.PathService;
import cn.am.service.PlanService;
import cn.kust.microtourismcommon.pojo.dto.MicroTourismUser;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.microtourismcommon.pojo.dto.TokenResult;
import cn.kust.microtourismcommon.remote.ServiceUserCenterClient;
import cn.kust.microtourismcommon.util.JwtUtils;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author : ArturiaMu KMUST-Stu
 * @Date : 2022/7/28 10:30
 * @Params : @param null
 * @Exception :
 * @Return :
 * @Description ：
 */
@Api(tags = "路径规划")
@RestController()
@RequestMapping("/pathService")
public class PathController {

    @Autowired
    private PlanService planService;
    @Resource
    private PathService pathPlanService;
    @Autowired
    private ServiceUserCenterClient serviceUserCenterClient;

    @PostMapping("/plan")
    public ResponseResult pathPlan(@RequestBody List<InterestPoint> interestPoints) {
        return pathPlanService.pathPlan(interestPoints);
    }

    @PostMapping("/savePlan")
    public ResponseResult<PlanVO> savePlan(@RequestBody PlanDTO plan, HttpServletRequest request) {
        TokenResult tokenResult = JwtUtils.parseRequestToken(request);
        ResponseResult<MicroTourismUser> userByPhone = serviceUserCenterClient.getUserByPhone(tokenResult.getPhone());
        PlanVO planVO = new PlanVO(null, plan.getPlanTime(), userByPhone.getData().getId(), userByPhone.getData().getAvatar(),
                userByPhone.getData().getUserName(), JSON.toJSONString(plan.getPlan()));
        return planService.save(planVO);
    }

    @GetMapping("/getAvailablePlan")
    public ResponseResult<List<PlanVO>> getAvailablePlan() {
        return planService.getAvailablePlan(new Date());
    }
}
